const db = uniCloud.database();
const uncCollection = db.collection('uni-notification-center')
// 公用模块用法请参考 https://uniapp.dcloud.io/uniCloud/cf-common
uncCollection.add = async (data)=>{
	// console.log('data',data);
	if(!data.content){
		throw new Error("content 不能为空")
	}
	if(!data.type_code){
		throw new Error("type_code 不能为空")
	}
	
	for(key in data){
		if(key == 'type_code'){
			if(typeof data[key] != 'number'){
				throw new Error(key+'的类型必须为：number')
			}
		}else if(key == 'to_all'){
			if(typeof data[key] != 'boolean'){
				throw new Error(key+'的类型必须为：number')
			}
		}else{
			if(typeof data[key] != 'string'){
				throw new Error(key+'的类型必须为：String')
			}
		}
	}

	if(data.to_all === undefined){
		data.to_all = false
	}
	if(data.to_all == false && data.user_id === undefined){
		throw new Error("非公告类型消息，user_id的值不能为空")
	}
	if(data.to_all == true && data.user_id != undefined){
		throw new Error("公告类型消息，user_id的值必须为 undefined ")
	}
	
	data.is_read = false
	data.create_time = Date.now()
	return await db.collection('uni-notification-center').add(data)
}
module.exports = uncCollection
